package com.zsc.graduate.qovipan.controller;

import com.zsc.graduate.qovipan.sql.UserOpService;
import com.zsc.graduate.qovipan.util.FileListInfo;
import com.zsc.graduate.qovipan.util.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.*;
import java.util.logging.Logger;


/**
 * @author 枫树下奶罐
 * @version 1.0.0
 * @date 2021/7/21 10:00
 */
@Controller
public class LoginController {
    private Logger log;
    @Autowired
    private UserOpService userOpService;

    @GetMapping("/userlogin")
    public String userLoginBack(@RequestParam(value = "logged",defaultValue = "") String logged, Model model) {

        if(logged.equals("false")) {
            log = Logger.getLogger("notLogged.Warn");
            log.info("进入错误提示");
            model.addAttribute("tips_warn", "color: red;");
            model.addAttribute("tips", "错误：用户未登录 或 GET请求不受支持");
        } else {
            log = Logger.getLogger("getMethod.reject");
            log.info("请不要使用get方法提交");
            model.addAttribute("tips_warn", "color: red;");
            model.addAttribute("tips", "请不要使用get方法提交");
        }
        return "login";
    }

    @PostMapping("/userlogin")
    public String userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            RedirectAttributes redirectAttr, Model model) {
        log = Logger.getLogger("postMethod.login.submit");
        log.info("提交成功");
        if(username.equals("")  || password.equals("")){
            log.info("账号或密码不能为空！");
            model.addAttribute("tips_warn", "color: red;");
            model.addAttribute("tips", "账号或密码不能为空！");
            return "login";
        }
//        此段代码：临时后台注册的账号，用于方便测试后续的页面
        else if(username.equals("123453") && password.equals("123453")){
            long QID = 123453;
            log.info("检测到临时账户，允许登录 (uid=" + QID + ")");
            return "forward:/user/" + QID;
        }
        else if(username.equals("root") && password.equals("admin147258")){
            log.info("检测到管理员账户，欢迎回来管理员。");
            return "redirect:/admin/";
        }
        else {
            User u = userOpService.findByName(username);
            if(u.getUsername().equals(username) && u.getPassword().equals(password)){
                return "forward:/user/" + u.getId();
            }
//            sql.getUser(username, password);
//            u.setUsername(username);
//            u.setPassword(password);
            log.info("用户名: " + username + " 密码: " + password);
            model.addAttribute("username", username);
            model.addAttribute("password", password);
            return "log";
        }
    }

    @GetMapping("/user/{QID}")
    public String userNotLoggedIn(@PathVariable String QID, RedirectAttributes redirectAttributes) {
        log = Logger.getLogger("getMethod.reject");
        log.info("请不要使用get方法提交");
        log = Logger.getLogger("notLoggedIn.reject");
        log.info("用户未登录，请先登录");
        redirectAttributes.addAttribute("logged","false");
        return "redirect:/userlogin";
    }

    @PostMapping("/user/{QID}")
    public String showFileList(@PathVariable String QID, Model model){
        log = Logger.getLogger("getUser.showFileList");
        log.info("正在获取该用户的文件 "+ QID);
//        这里获取用户的文件列表信息
//        FilePlayController flController = new FilePlayController();
        List<String> fHead = new ArrayList<>();
        fHead.add("文件名");
        fHead.add("修改日期");
        fHead.add("文件类型");
        fHead.add("大小");
        //        List<String> fList = new ArrayList<>();
        List<FileListInfo> fList = new ArrayList<>();
        FileListInfo[] flInfo = new FileListInfo[999];
        for (int i = 0; i < 999; i++) {
            flInfo[i] = new FileListInfo();
        }
        if(QID.equals("123453")) {

            flInfo[0].setAll(1, "1.jpg", "image/jpeg", 0, 123453, "");
            flInfo[1].setAll(2, "2.mp4", "video/mp4", 0, 123453, "");
            flInfo[2].setAll(3, "3.mp3", "audio/mp3", 0, 123453, "");
            flInfo[3].setAll(4, "4.txt", "text/txt", 0, 123453, "");
            flInfo[4].setAll(5, "5.png", "image/png", 0, 123453, "");
            for (int i = 0; flInfo[i].getItemId() != null; i++) {
                fList.add(flInfo[i]);
            }
            for (FileListInfo flInfos : fList) {
                log.info(flInfos.printAll());
            }
            //        for(String string: fList){ log.info(string); }
        }
        model.addAttribute("fileList", fList);
        model.addAttribute("fileHead", fHead);
        model.addAttribute("QID", QID);
        return "file-list";
    }
}
